package ua.zwpp.java8.methodreferences;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

/**
 * Created by vitaly on 16.09.14.
 *
 * Cheatsheet at http://www.java8.org/
 */
public class Demo {
    ThreadLocal<StringBuilder> localTextBuffer = ThreadLocal.withInitial(StringBuilder::new); // case 4

    public static void main(String[] args) {
        Demo d = new Demo();
        d.case1();
        d.case2();
        d.case3();
    }

    private void case3() {
        print(Arrays.asList("Scala", null, "Java", null, "Clojure"), Objects::nonNull); //class::classM
    }

    private void print(List<String> langs, Predicate<String> predicate) {
        langs.forEach(s -> {if (predicate.test(s)) System.out.println(s);});
    }

    private void case2() {
        Arrays.asList("Scala", "C++", "Java", "Clojure", "F#").forEach(System.out::println); // instance::instanceM
    }

    private void case1() {
        String[] names = { "Barbara", "James", "Mary", "John",
                "Patricia", "Robert", "Michael", "Linda" };

        Arrays.sort(names, String::compareToIgnoreCase); // class::instanceM

        System.out.println(Arrays.asList(names));
    }
}
